<?php
/**
 * Created by JetBrains PhpStorm.
 * User: developer2
 * Date: 8/30/12
 * Time: 10:53 AM
 * To change this template use File | Settings | File Templates.
 */
class Page_Be_Cms_Settings extends Page
{
    /**
     * @var Db_Cms_Settings
     */
    protected $tableObject;

	function __construct(){
        $this->idTableObject = get('id', 'int');
        $this->actionsTableObject = array(
            'addEditAction',
            'saveAction'
		);
        $this->classTableObject = 'Db_Cms_Settings';
		parent::__construct();
	}

	public function defaultAction(){
        if (perm('SETTINGS_EDIT')) {
            $this->addBreadcrumb(t(T_FORM_BUTTON, 'add'), '?action=addEdit');
        }
		$allProjectsSettings = Db_Cms_Settings::getProjectSettings();
		$allProjects  = Db_Projects::getDistinctProjects();
		self::$TPL->assign('allProjects', $allProjects);
		self::$TPL->assign('allSettings',$allProjectsSettings);
		self::$TPL->setBodyTemplate('list_settings');
		return array(
			'type' => 'display'
		);
	}

	public function addEditAction(){
		perm('SETTINGS_EDIT', '/settings');
		if(!empty($this->idTableObject)){
			$this->addBreadcrumb(t(T_FORM_BUTTON, 'edit') .' &quot;' . $this->tableObject->objectArray->s_key . '&quot;', '?action=addEdit&id=' . $this->idTableObject, true);
			self::$TPL->assign('currentSetting', $this->tableObject->objectArray);
		} else {
			$this->addBreadcrumb(t(T_FORM_BUTTON, 'add'), '?action=addEdit', true);
		}
		$allProjects = Db_Projects::getAllProjects();
		self::$TPL->assign('allProjects', $allProjects);
		self::$TPL->setBodyTemplate('add_edit_setting');
		return array(
			'type' => 'display'
		);
	}

	public function saveAction(){
		perm('SETTINGS_EDIT', '/settings');
		$projectId = post('pr_id','int');
		$settingTitle = strtolower(post('s_title','string'));
		$settingKey = strtolower(post('s_key','string'));
		$settingContent = post('s_content','string',true);

		if(!empty($settingTitle) && !empty($settingKey) && !empty($settingContent)){
			if($this->idTableObject){
				$this->tableObject->id = $this->idTableObject;
			}
			$this->tableObject->s_project_id = $projectId;
			$this->tableObject->s_title = $settingTitle;
			$this->tableObject->s_key= $settingKey;
			$this->tableObject->s_content = $settingContent;

			if(!$this->tableObject->save()){
				$this->error = true;
				$this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
			}
		} else {
			$this->error = true;
			$this->message = t(T_FORM_ERROR, 'required_fields');
		}
        $this->setUserMessage();

			return array(
				'type' => 'redirect',
				'query' => false
			);
	}

	public function deleteAction() {
		perm('SETTINGS_EDIT', '/settings');
		$this->message = t(T_FORM_MSG, 'success');
		if(!empty($this->idTableObject)) {
			if(!Db_Cms_Settings::abstractDelete($this->idTableObject)) {
				$this->error = true;
				$this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
			}
		} else {
			$this->error = true;
			$this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
		}
		return array(
			'type' => 'redirect',
			'query' => false
		);
	}
}